package com.pangda.common.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class UsexConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.NUMBER;
    }

    @Override
    public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 导入时字符串转换数字
        switch (cellData.getStringValue()){
            case "男" : return 1;
            case "女" : return 0;
            default: return -1;
        }
    }

    /**
     * 将从数据库中查到的数据转换为 Excel 展示的数据
     * @param value 枚举对象
     */
    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String sexName;
        switch (value){
            case 0 : sexName= "女";break;
            case 1 : sexName= "男";break;
            default: sexName="其他";
        }
        return new WriteCellData<>(sexName);
    }
}
